package com.example.figurehecheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.domain.dto.VenuesDTO;
import com.example.figurehecheng.domain.entity.Image;
import com.example.figurehecheng.domain.entity.Restaurant;
import com.example.figurehecheng.domain.entity.Scenery;
import com.example.figurehecheng.domain.entity.Venues;
import com.example.figurehecheng.domain.vo.VenuesVO;
import com.example.figurehecheng.mapper.VenuesMapper;
import com.example.figurehecheng.service.IImageService;
import com.example.figurehecheng.service.IVenuesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 场馆信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-04
 */
@Service
public class VenuesServiceImpl extends ServiceImpl<VenuesMapper, Venues> implements IVenuesService {
    @Autowired
    private IImageService imageServiceImpl;
    @Autowired
    private IVenuesService venuesService;

    /**
     * 创建场馆
     *
     * @param dto
     * @return
     */
    @Override
    public Long createVenues(VenuesDTO dto) {
        // 获取当前登录用户ID
        Long currentId = BaseContext.getCurrentId();
        Venues venues = new Venues();
        BeanUtils.copyProperties(dto, venues);
        venues.setCreatedAt(LocalDateTime.now());
        venues.setUpdatedAt(LocalDateTime.now());
        venues.setMid(currentId.intValue());
        venues.setStatus(1);
        save(venues);
        return venues.getId();
    }

    /**
     * 获取商家场馆列表
     *
     * @return
     */
    @Override
    public List<VenuesVO> getVenuesList() {
        Integer mid = venuesService.lambdaQuery().one().getMid();
        LambdaQueryWrapper<Venues> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Venues::getMid, mid);
        List<Venues> venueslist = list(queryWrapper);

        return venueslist.stream().map(venues -> {
            VenuesVO vo = new VenuesVO();
            BeanUtils.copyProperties(venues, vo);
            //查询关联的图片列表
            List<Image> images = imageServiceImpl.lambdaQuery()
                    .eq(Image::getRelationId, venues.getMid())
                    .list();
            if (images != null && !images.isEmpty()) {
                // 将图片列表转换为图片URL列表
                List<String> imageUrls = images.stream()
                        .map(Image::getUrl)
                        .collect(Collectors.toList());
                vo.setCoverImage(imageUrls.get(0));
            }
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取商家场馆详情
     *
     * @return
     */
    @Override
    public VenuesVO getVenuesByMid() {
        int currentId = BaseContext.getCurrentId().intValue();
        System.out.println("currentId"+currentId);
        Integer mid = currentId;
        LambdaQueryWrapper<Venues> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Venues::getMid, mid);
        Venues venues = getOne(queryWrapper);
        if (venues == null) {
            return null;
        }

        //查询关联的图片列表
        List<Image> images = imageServiceImpl.lambdaQuery()
                .eq(Image::getRelationId, venues.getMid())
                .list();
        // 将Venues对象转换为VenuesVO对象
        VenuesVO vo = new VenuesVO();
        BeanUtils.copyProperties(venues, vo);
        if (images != null && !images.isEmpty()) {
            // 将图片列表转换为图片URL列表
            List<String> imageUrls = images.stream()
                    .map(Image::getUrl)
                    .collect(Collectors.toList());
            vo.setImages(imageUrls);
        }
        System.out.println("Venues"+venues);
        System.out.println(vo);
        return vo;
    }

    /**
     * 修改商家场馆信息
     *
     * @param dto
     * @return
     */
    @Override
    public boolean updateVenues(VenuesDTO dto) {
        int currentId = BaseContext.getCurrentId().intValue();
        System.out.println("currentId"+currentId);
        Integer mid = currentId;
        LambdaQueryWrapper<Venues> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Venues::getMid, mid);
        Venues oldvenues = getOne(queryWrapper);
        if (oldvenues == null) {
            throw new RuntimeException("场馆不存在或无权修改");
        }
        // 复制属性并更新
        Venues venues = new Venues();
        BeanUtils.copyProperties(dto, venues);
        venues.setUpdatedAt(LocalDateTime.now());
        venues.setStatus(oldvenues.getStatus());
        return updateById(venues);
    }
    /**
     * 获取商家场馆详情
     *
     * @return
     */
    @Override
    public VenuesVO getVenuesVODetail(Long id) {
        // 根据id查询Venues对象
        Venues venues = getById(id);
        if (venues == null) {
            throw new RuntimeException("场馆不存在");
        }
        //转化为VenuesVO对象
        VenuesVO vo = new VenuesVO();
        BeanUtils.copyProperties(venues, vo);
        if (vo != null) {
            // 查询关联的图片列表
            List<Image> images = imageServiceImpl.lambdaQuery()
                    .eq(Image::getRelationId, venues.getMid())
                    .list();
            if (images != null && !images.isEmpty()) {
                // 将图片列表转换为图片URL列表
                List<String> imageUrls = images.stream()
                        .map(Image::getUrl)
                        .collect(Collectors.toList());
                vo.setImages(imageUrls);
            }
        }
        return vo;
    }

    /**
     * 根据名称搜索场馆
     *
     * @param name
     * @return
     */
    @Override
    public List<VenuesVO> searchVenuesByName(String name) {
        LambdaQueryWrapper<Venues> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Venues::getName,  "%" + name + "%");
        List<Venues> venuesList = list(queryWrapper);
        return venuesList.stream().map(venues -> {
            VenuesVO vo = new VenuesVO();
           BeanUtils.copyProperties(venues, vo);
            // 查询关联图片
            List<Image> images = imageServiceImpl.lambdaQuery()
                    .eq(Image::getRelationId, venues.getMid())
                    .list();
            if (images != null && !images.isEmpty()) {
                vo.setCoverImage(images.get(0).getUrl());
            }
            return vo;
        }).collect(Collectors.toList());
    }
}
